package com.bwie.spu.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.bwie.common.core.utils.DateUtils;
import com.bwie.common.core.utils.bean.BeanUtils;
import com.bwie.spu.domain.SkuEsModel;
import com.bwie.spu.domain.dto.ProductUpDTO;
import com.bwie.spu.domain.po.*;
import com.bwie.spu.domain.vo.AttrVo;
import com.bwie.spu.domain.vo.ProductVo;
import com.bwie.spu.remote.EsRemote;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.bwie.spu.mapper.PmsSpuInfoMapper;
import com.bwie.spu.service.IPmsSpuInfoService;
import org.springframework.transaction.annotation.Transactional;

/**
 * spu信息Service业务层处理
 *
 * @author bwie
 * @date 2024-12-18
 */
@Service
public  class PmsSpuInfoServiceImpl implements IPmsSpuInfoService
{
    @Autowired
    private PmsSpuInfoMapper pmsSpuInfoMapper;
    @Autowired
    private EsRemote esRemote;

    /**
     * 查询spu信息
     *
     * @param id spu信息主键
     * @return spu信息
     */
    @Override
    public ProductPo selectPmsSpuInfoById(Integer id)
    {
        return pmsSpuInfoMapper.selectPmsSpuInfoById(id);
    }

    /**
     * 查询spu信息列表
     *
     * @param productPo spu信息
     * @return spu信息
     */
    @Override
    public List<ProductVo> selectPmsSpuInfoList(ProductPo productPo)
    {
        //创建返回值
        List<ProductVo> productVos = new ArrayList<>();
        //搜索列表
        List<ProductPo> productPos = pmsSpuInfoMapper.selectPmsSpuInfoList(productPo);
        //循环
        productPos.forEach(getProductPo -> {
            //创建projectVo对象
            ProductVo productVo = new ProductVo();
            //复制
            BeanUtils.copyBeanProp(productVo,getProductPo);
            //判断
            if (productVo.getPublishStatus()==0){
                //赋值
                productVo.setPublishStatusName("下架");
            }else{
                //赋值
                productVo.setPublishStatusName("上架");
            }
            productVos.add(productVo);
        });
        return productVos;
    }

    /**
     * 新增spu信息
     *
     * @param productPo spu信息
     * @return 结果
     */
    @Override
    public int insertPmsSpuInfo(ProductPo productPo)
    {
        productPo.setCreateTime(DateUtils.getNowDate());
        return pmsSpuInfoMapper.insertPmsSpuInfo(productPo);
    }

    /**
     * 修改spu信息
     *
     * @param productPo spu信息
     * @return 结果
     */
    @Override
    public int updatePmsSpuInfo(ProductPo productPo)
    {
        productPo.setUpdateTime(DateUtils.getNowDate());
        return pmsSpuInfoMapper.updatePmsSpuInfo(productPo);
    }

    /**
     * 批量删除spu信息
     *
     * @param ids 需要删除的spu信息主键
     * @return 结果
     */
    @Override
    public int deletePmsSpuInfoByIds(Integer[] ids)
    {
        return pmsSpuInfoMapper.deletePmsSpuInfoByIds(ids);
    }

    /**
     * 删除spu信息信息
     *
     * @param id spu信息主键
     * @return 结果
     */
    @Override
    public int deletePmsSpuInfoById(Integer id)
    {
        return pmsSpuInfoMapper.deletePmsSpuInfoById(id);
    }

    /**
     * 上架功能
     * @param productUpDTO
     */
    @Transactional
    @Override
    public void productUp(ProductUpDTO productUpDTO) {
        //根据spuid 获得sku列表
        List<SkuPo> skuPos = pmsSpuInfoMapper.getSkuListBySpuId(productUpDTO);
        //重构上传到es的模型
        List<SkuEsModel> skuEs = getSkuEs(skuPos, productUpDTO.getId());
        //上传到es
        esRemote.up(skuEs);
        //修改spu状态
        pmsSpuInfoMapper.updatePublishStatus(productUpDTO.getId());

    }



    public List<SkuEsModel>getSkuEs(List<SkuPo> skuPos,Integer spuId){
        //根据spuid获得属性列表
        List<AttrEsPo> attrEsPos = pmsSpuInfoMapper.getAttrEsPo(spuId);

        List<AttrVo> attrVoList = attrEsPos.stream().map(attrEsPo -> {
            AttrVo attrVo = new AttrVo();
            attrVo.setAttrId(attrEsPo.getAttrId().intValue());
            attrVo.setAttrName(attrEsPo.getAttrName());
            attrVo.setAttrValue(attrEsPo.getAttrValue());

            return attrVo;
        }).collect(Collectors.toList());

        List<SkuEsModel> skuEsModels = skuPos.stream().map(skuPo -> {
            SkuEsModel skuEsModel = new SkuEsModel();
            skuEsModel.setSkuId(skuPo.getSkuId().longValue());
            skuEsModel.setSpuId(skuPo.getSpuId().longValue());
            skuEsModel.setSkuTitle(skuPo.getSkuTitle());
            skuEsModel.setSkuImg(skuPo.getSkuDefaultImg());
            skuEsModel.setSaleCount(skuPo.getSaleCount().longValue());
            skuEsModel.setHotScore(Long.valueOf(100));
            skuEsModel.setHasStock(true);
            skuEsModel.setSkuPrice(skuPo.getPrice());

            BrandPo brandPo = pmsSpuInfoMapper.getBrandPo(skuPo.getBrandId().intValue());
            skuEsModel.setBrandId(brandPo.getBrandId().longValue());
            skuEsModel.setBrandName(brandPo.getName());
            skuEsModel.setBrandImg(brandPo.getLogo());

            CategoryPo categoryPo = pmsSpuInfoMapper.getCategoryPo(skuPo.getCatalogId());
            skuEsModel.setCatalogId(categoryPo.getCatId().longValue());
            skuEsModel.setCatalogName(categoryPo.getName());

            skuEsModel.setAttrs(attrVoList);

            return skuEsModel;
        }).collect(Collectors.toList());


        return skuEsModels;
    }
}

